package com.edclol.work;

import com.edclol.work.pojo.KafkaBean;
import com.google.gson.Gson;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer011;

import java.util.Properties;

/**
 * @ClassName: SinkTest1_Kafka
 * @Description:
 * @Author: jesse on 2020/11/9 10:24
 * @Version: 1.0
 */
public class TableTest1_ReadKafka {
    public static void main(String[] args) throws Exception{

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "121.37.179.13:9092,124.71.186.17:9092,124.70.143.1:9092");
        properties.setProperty("group.id", "xialonglong2-test-consumer-group");
//        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
//        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
//        properties.setProperty("auto.offset.reset", "latest");
        properties.setProperty("auto.offset.reset", "earliest");

        // 读取数据
        DataStream<String> inputStream = env.addSource( new FlinkKafkaConsumer011<String>("nj_realr_time_target", new SimpleStringSchema(), properties));

        SingleOutputStreamOperator<KafkaBean> dataStream = inputStream.map(line -> {
            Gson gson = new Gson();
            KafkaBean bean = gson.fromJson(line, KafkaBean.class);
            return bean;
        });

        dataStream.map(new MapFunction<KafkaBean, Object>() {
            @Override
            public Object map(KafkaBean value) throws Exception {
                System.out.println(value);
                return null;
            }
        }).print();

//        dataStream.print();

        env.execute();
    }
}
